const { getUrl } = require("../../utils/url");

const app = getApp()
let _this;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address:'',
    no:'',
    page:0,
    cai:false,
    total_fee:0,
    address:'',
    link:'',
    file:"",
    price:[],
    des:"",

  },
  //备注信息
  desTapinput(e){
    let {value}=e.detail;
    this.setData({
      des:value
    })
  }
  ,

  navTo(e) {
    app.com.navTo(e)
  },
  chooseFile(){
    wx.chooseImage({
      success (res) {
        const tempFilePaths = res.tempFilePaths[0];
        _this.setData({
          file:tempFilePaths
        })
    }
    })
   
  },
  //更新参数的连接地址的值
  linkaddress(e){
     let {value}=e.detail;
     this.setData({
       link:value
     })

  },
  //链接复制事件
  fzwp(){
    wx.setClipboardData({
      data: 'https://pc.woozooo.com/',
      success (res) {
        wx.getClipboardData({
          success (res) {
            wx.showToast({
              title: '连接复制成功',
            })
          }
        })
      }
    })

  },

  pageInput(e){
    this.data.page = e.detail.value
    this.init()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    _this = this
    if (wx.getStorageSync("address")) {
      let add = wx.getStorageSync("address")[0]
      this.setData({
        address: add.address + '-' + add.detail
      })
    }
    wx.request({
      url: getUrl()+`/printprice/findAll`,  //路径
      success: function(res) {
       _this.setData({
         price:res.data
       })
      }
    }) 

  },
  onShow(){
  },
  getFile(){

 },
 //提交表单
  formSubmit(e){
     //判断上传文件没有
    if(!this.data.file == ''){
   let {file}= this.data;
    wx.uploadFile({
      url: getUrl()+`/file/updoladfile`, 
      filePath: file,
      name:'file',
      header: {
        'Content-Type': 
         'json'
        },
      success (res){
        _this.postSave();
      }
    })
    }else if(this.data.address == ''){
      wx.showToast({
        title: '请选择一个地址',
        icon:'none'
      })
    } else if (e.detail.value.page == '' || e.detail.value.page == null){
      wx.showToast({
        title: '请输入页数',
        icon: 'none'
      })
    }else {
      wx.showLoading({
        title: '加载中',
      })
  this.postSave();
}
  },
  postSave(){
    let OrderS={
      userId:wx.getStorageSync('user').id,//发布用户id
      money:this.data.total_fee,  //发布价格
      title:this.data.des,  //备注
      addressId:wx.getStorageSync('address')[0].id,//地址得id
      schoolId:wx.getStorageSync('area').id,//学校id
      pages:this.data.page,//当前页数
      link:this.data.link//链接地址
  }
  wx.request({
   url: getUrl()+`/file/updolad`,  //路径
   data:OrderS,//参数
   success: function(res) {
   if(res.data.code==200){
    wx.showToast({
      title: '发布成功',
    })
   }else{
     wx.showToast({
       title: '发布失败',
       icon:"none"
     })
   }
  
   }
 })


  },

  tempUp(){
    app.com.post('file/temp',{
      id:this.data.file.id
    },function(res){

    })
  },
  wxpay(msg) {
    app.com.wxpay(msg)
  },
  switch1Change(e){
    this.setData({
      cai:e.detail.value
    })
    this.init()
  },  
  //计算价格
  init(){
    let {price}=this.data;
    let {page}=this.data;
     let total_price=this.data.cai===true?price.colorprinting*page+price.errandcosts:price.errandcosts+page*price.printing;
     this.setData({
       total_fee:total_price
     })
  }
})